﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;
using AG.Utilities;

namespace SQLServerHealthCheck.SQLServer.IssueTypes
{
    public class DatabaseAutoGrowthOff : IssueTypeCheckBase
    {
        public DatabaseAutoGrowthOff()
        {
            IssueTypeId = 3017;
        }

        public override void Check()
        {
            try
            {
                HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);
                Issue issue;
                
                var qry = from d in dc.Databases
                          from df in d.DatabaseFiles

                          where df.FileGroup == "PRIMARY"
                          && !df.AutoGrowth.Value
                          && d.SourceDatabaseId == null


                          select df;
                //Get Issuetype object  And Create a new  Global issue object 

                IssueType issueType = dc.IssueTypes.FirstOrDefault(i => i.IssueTypeId == IssueTypeId);

                foreach (var item in qry)
                {
                    //Get Issuetype object


                    

                    issue = new Issue();

                    issue.IssueType = issueType;
                    issue.TargetId = item.Database.DatabaseID;
                    issue.Description = issueType.Description.Replace("{Database}", item.Database.DatabaseName);

                    dc.Issues.InsertOnSubmit(issue);
                }

                dc.SubmitChanges();

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }


    }








}
